package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.ShopComment;
import com.woniuxy.common.entity.ShopCommentExample;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.ShopCommentMapper;
import com.woniuxy.joylifeshop.exception.CommentException;
import com.woniuxy.joylifeshop.exception.CommodityException;
import com.woniuxy.joylifeshop.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    ShopCommentMapper shopCommentMapper;

    @Autowired
    CenterUserMapper centerUserMapper;

    /**
    * @Author SooYoo
    * @Description 添加一条评论
    * @Param
    * @Return
    **/
    @Override
    public void addComment(String userId, String shopId,String content) {

        try {
            ShopComment shopComment = new ShopComment();
            shopComment.setCommentId(UUID.randomUUID().toString());
            shopComment.setComId(shopId);
            shopComment.setId(userId);
            shopComment.setData(new Date());
            //找到用户名字
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(userId);
            shopComment.setUsername(centerUser.getName());
            shopComment.setContent(content);

            shopCommentMapper.insert(shopComment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommentException();
        }


    }


    /**
    * @Author SooYoo
    * @Description 找到某comid的所有评论对象
    * @Param
    * @Return
    **/
    @Override
    public List<ShopComment> readAllCommentByComid(String comId) {

        try {
            ShopCommentExample shopCommentExample = new ShopCommentExample();
            ShopCommentExample.Criteria criteria = shopCommentExample.createCriteria();
            criteria.andComIdEqualTo(comId);
            List<ShopComment> shopComments = shopCommentMapper.selectByExample(shopCommentExample);

            return shopComments;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommodityException();
        }
    }


}
